package cn.liuhuan1.blogsModel.service;


import cn.liuhuan1.blogsModel.dao.SlideshowDao;
import cn.liuhuan1.entity.PageResult;
import cn.liuhuan1.webModelBean.api.SlideshowImp;
import cn.liuhuan1.webModelBean.bo.SlideshowBo;
import cn.liuhuan1.webModelBean.vo.SlideshowVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SlideshowService implements SlideshowImp {
    @Autowired
    public SlideshowDao slideshowDao;

    @Override
    public List<SlideshowVo> getSlideshow(){
        List<SlideshowVo> slideshowVoList = slideshowDao.getSlideshow();
        return slideshowVoList;
    }

    @Override
    public PageResult<SlideshowVo> getPageSlideShow(SlideshowBo slideshowBo) {

        if(slideshowBo.getPageCurrent() < 1){
            slideshowBo.setPageCurrent(0);
        }else{
            slideshowBo.setPageCurrent(slideshowBo.getPageCurrent() - 1);
        }
        if(slideshowBo.getPageSize() < 9){
            slideshowBo.setPageCurrent(10);
        }

        PageResult<SlideshowVo> pageResult = new PageResult<>();

        List<SlideshowVo> slideshowVoList = slideshowDao.getPageSlideShow(slideshowBo);

        int count = slideshowDao.getPageCount(slideshowBo);

        pageResult.setPageList(slideshowVoList);

        pageResult.setPageShowTotal(count);
        pageResult.setPageCurrent(slideshowBo.getPageCurrent());
        pageResult.setPageSize(slideshowBo.getPageSize());
        return pageResult;
    }

    @Override
    public void delSlideShow(String id) {
        if(id != null && id.trim() != ""){
            slideshowDao.delSlideShow(id);
        }else{
            throw new NullPointerException();
        }

    }

    @Override
    public void saveSlideShow(SlideshowBo slideshowBo) {
        slideshowDao.saveSlideShow(slideshowBo);
    }

    @Override
    public SlideshowVo getSlideShowById(String id) {
        if(id != null && id.trim() != ""){
            SlideshowVo slideshowVo = slideshowDao.getSlideShowById(id);
            if(slideshowVo != null){
                return slideshowVo;
            }
        }
        throw new NullPointerException();
    }

    @Override
    public void updateSlideShow(SlideshowBo slideshowBo) {
        slideshowDao.updateSlideShow(slideshowBo);
    }
}
